package com.lzy.business.controller.excel;

import com.github.pagehelper.PageInfo;
import com.lzy.business.controller.BaseController;
import com.lzy.business.request.MemberCheckLogRequest;
import com.lzy.business.response.excel.CheckLogResponse;
import com.lzy.coupon.dtos.PromotionMemberDto;
import com.lzy.framework.core.excel.SimpleExcelExportUtils;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.CollectionUtils;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.core.utils.WebUtils;
import com.lzy.member.apis.MemberCheckLogServiceApi;
import com.lzy.member.dtos.MemberCheckLogDto;
import com.lzy.web.exception.GenericException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by WIN7 on 2018/4/8.
 */
@Controller
@RequestMapping("/excel")
public class ExcelExportController extends BaseController{

    protected Log logger = LogFactory.getLog(ExcelExportController.class);

    @Autowired
    MemberCheckLogServiceApi memberCheckLogServiceApi;


//    //活动名单导出
//    @RequestMapping(value = "/checkLogList")
//    public void exportCheckLogList(HttpServletRequest request, HttpServletResponse response, MemberCheckLogRequest checkLogRequest) {
//        try{
//            Pager pager = new Pager();
//            pager.setPageSize(10000);
//            MemberCheckLogDto dto = DozerUtils.map(checkLogRequest, MemberCheckLogDto.class);
//            PageInfo<MemberCheckLogDto> pageInfo = memberCheckLogServiceApi.selectCheckLogList(pager,dto);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            List<MemberCheckLogDto> list = pageInfo.getList();
//            if (CollectionUtils.isNotEmpty(list)) {
//                if (list.size() > 10000)
//                    throw new GenericException("导出数据量过大,请重新输入检索条件,分批导出！");
//                LinkedList<CheckLogResponse> voList = new LinkedList<>();
//                CheckLogResponse vo;
//                for (MemberCheckLogDto memberCheckLogDto : list) {
//                    vo = new CheckLogResponse();
//                    BeanUtils.copyProperties(memberCheckLogDto, vo);
//                    voList.add(vo);
//                }
//                SimpleExcelExportUtils excelExportService = new SimpleExcelExportUtils();
//                HSSFWorkbook wb = new HSSFWorkbook();
//                if (voList != null && voList.size() > 0) {
//                    wb = excelExportService.generateExcel(voList);
//                }
//                String fileName = "核销记录报表" + sdf.format(new Date()) + ".xls";
//                // 设定输出文件头
//                WebUtils.prepareDownloadFile(fileName, "application/msexcel", request, response);
//                OutputStream out = response.getOutputStream();
//                wb.write(out);
//                out.close();
//            } else {
//                logger.error("没有可执行数据：核销记录数据导出EXCEL失败！");
//            }
//        }catch (Exception e){
//            logger.error("核销记录数据导出EXCEL失败！", e);
//            throw new GenericException("核销记录数据导出EXCEL失败");
//        }
//    }


}
